package com.zjsru.oneDay;

/**
 * 最长公共前缀
 * <p>
 * 输入：strs = ["flower","flow","flight"]
 * 输出："fl"
 */
public class longestCommonPrefix {
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0)
            return "";
        String s = strs[0];
        for (String string : strs) {
            while (!string.startsWith(s)) { //startsWith 对比s是否与String的值相等
                if (strs.length == 0)
                    return "";
                s = s.substring(0, s.length() - 1); //substring 获取0到 length（）-1的值
            }
        }
        return s;
    }
    public static void main(String[] args) {
        String[] strs = new String[]{"flower", "flow", "flight"};
        longestCommonPrefix longestCommonPrefix = new longestCommonPrefix();
        System.out.println(longestCommonPrefix.longestCommonPrefix(strs));
    }
}
